<template>
  <div class="contentView" v-loading="loading" :style="`height:${contentHeight}px`">
    <div class="routeHeader mb-5px flex">
      <el-button
        v-if="formData.debuggingOrderStatus !== 30"
        type="primary"
        @click="submitForm('save')"
        v-hasPermi="['produce:debugger-order:save']"
        :disabled="loading"
      >
        保 存
      </el-button>
      <el-button
        v-if="formData.debuggingOrderStatus !== 30"
        type="primary"
        @click="submitForm('render')"
        v-hasPermi="['produce:debugger-order:submit']"
        :disabled="loading"
      >
        提 交
      </el-button>
      <el-button class="ml-12px" @click="goBack">关 闭</el-button>
    </div>
    <el-table
      ref="multipleTableRef"
      v-loading="loading"
      :data="[formData]"
      border
      :max-height="450"
      class="table-style"
    >
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="任务单号" align="center" prop="workTaskNumber" />
      <el-table-column label="产品编号" align="center" prop="productNumber" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品规格" align="center" prop="productStandard" />
      <el-table-column label="订单数量" align="center" prop="orderQuantity" />
      <!-- <el-table-column label="图纸图号" align="center" prop="drawingNumber">
        <template #default="scope">
          <el-button
            class="drawingBtn"
            type="primary"
            link
            @click="jumpPdfFile(scope.row.drawingNumber)"
            >{{ scope.row.drawingNumber }}</el-button
          >
        </template>
      </el-table-column> -->
      <el-table-column label="完工日期" align="left" header-align="left" prop="completeDate" />
      <el-table-column label="调试完工单" align="left" header-align="left">
        <template #default="scope">
          <div v-if="scope.row.debuggingOrderStatus !== 30">
            <UploadFileAdd
              v-model="fileList"
              :isShowTip="false"
              :limit="1"
              :fileSize="200"
              :fileType="['pdf', 'png', 'jpg']"
              @update:modelValue="uploadFileRes"
            />
          </div>
          <div v-else>{{ scope.row.fileName }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="left" header-align="left" prop="debuggingOrderStatus">
        <template #default="scope">
          <dict-tag type="completion_order_status" :value="scope.row.debuggingOrderStatus" />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script setup lang="ts">
// import { jumpPdfFile } from '@/utils/formCreate'
import * as DebuggerOrderApi from '@/api/produce/processManagement/debuggerOrder'
import { useTagsViewStore } from '@/store/modules/tagsView'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const { query } = useRoute() // 路由的查询
const loading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 修改  view - 查看
const fileList = ref<any[]>([])
const formData = ref({
  id: undefined,
  workTaskNumber: undefined,
  productNumber: undefined,
  productName: undefined,
  productStandard: undefined,
  orderQuantity: undefined,
  drawingNumber: undefined,
  debuggingOrderStatus: undefined,
  completeDate: undefined,
  debuggingOrderUrl: undefined,
  creatorName: undefined,
  fileId: undefined,
  filePath: '',
  fileName: ''
})

/** 打开弹窗 */
const getData = async () => {
  const id = Number(query.id)
  formType.value = (query as any).type
  // 修改时，设置数据
  if (id) {
    loading.value = true
    try {
      formData.value = await DebuggerOrderApi.getDebuggerOrder(id)
      if (
        formData.value.fileName !== '' &&
        formData.value.fileName !== null &&
        formData.value.fileName !== undefined
      ) {
        fileList.value.push({
          name: formData.value.fileName,
          url: formData.value.filePath
        })
      }
    } finally {
      loading.value = false
    }
  }
}

/** 提交表单 */
const submitForm = async (val: string) => {
  // 提交请求
  loading.value = true
  try {
    const data = formData.value as unknown as DebuggerOrderApi.DebuggerOrderVO
    if (!formData.value.fileName) {
      return message.notifyError('请上传调试完工单')
    }
    if (val === 'save') {
      await DebuggerOrderApi.updateDebuggerOrder(data)
      message.success(t('common.createSuccess'))
      goBack()
    } else {
      await DebuggerOrderApi.submitDebuggerOrder(data)
      message.success(t('common.updateSuccess'))
      goBack()
    }
  } finally {
    loading.value = false
  }
}

/**文件上传返回 */
const uploadFileRes = (data) => {
  if (JSON.parse(data).length) {
    const res = JSON.parse(data)
    formData.value.fileName = res[0].name
    formData.value.filePath = res[0].url
  } else {
    fileList.value = []
    formData.value.fileName = ''
    formData.value.filePath = ''
  }
}

/** 关闭 */
const tagsViewStore = useTagsViewStore()
const route = useRouter() // 路由
const goBack = async () => {
  //获取当前路由并删除tagView
  tagsViewStore.delView(route.currentRoute.value)
  //跳转列表页
  route.push({
    path: '/produce/produce/processManagement/debuggerOrder'
  })
}

const contentHeight = ref(0)
onMounted(() => {
  contentHeight.value = document.body.clientHeight - 127
  getData()
})
</script>
